package org.opens.controller;

import org.opens.entity.RedisCacheEntity;
import org.opens.entity.RedisCacheSaveDTO;
import org.opens.entity.RedisCacheUpdateDTO;
import org.opens.service.RedisCacheService;
import org.opens.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 测试redis的缓存功能
 *
 * @author zym
 * @Date 2022/6/28 15:34
 */
@RequestMapping(value = "/api/cache")
@RestController
public class RedisCacheController {

    @Autowired
    private RedisCacheService redisCacheService;

    @RequestMapping(value = "/save")
    public R<RedisCacheEntity> save(@Valid @RequestBody RedisCacheSaveDTO redisCacheSaveDTO) {
        RedisCacheEntity saveEntity = redisCacheService.save(redisCacheSaveDTO);
        return R.success(saveEntity);
    }

    @RequestMapping(value = "/delete/{id}")
    public R<Boolean> delete(@PathVariable(value = "id") Integer id) {
        boolean deleteRes = redisCacheService.delete(id);
        return R.success(deleteRes);
    }

    @RequestMapping(value = "/update")
    public R<RedisCacheEntity> update(@Valid @RequestBody RedisCacheUpdateDTO redisCacheUpdateDTO) {
        RedisCacheEntity updateEntity = redisCacheService.update(redisCacheUpdateDTO);
        return R.success(updateEntity);
    }

    @RequestMapping(value = "/select/{id}")
    public R<RedisCacheEntity> select(@PathVariable(value = "id") Integer id) {
        RedisCacheEntity selectEntity = redisCacheService.selectById(id);
        return R.success(selectEntity);
    }

}
